<template>
  <div>
      <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row>
          <el-col>
              <el-button @click="add">添加商品</el-button>
          </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table border :selection-type="false" index-text="#" show-index :data="GoodsList" :columns="columns">
          <!-- 是否生效 -->
        <template slot="isOk" slot-scope="scope">
            <i class="el-icon-success" v-if="scope.row.cat_deleted=true" style="color: lightgreen"></i>
            <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
            <el-tag type="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
            <el-tag type="error" v-else-if="scope.row.cat_level === 2">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="operat" slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editOpen(scope.row.cat_id)" circle>编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteCate(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1, 3, 5, 10]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </el-card>
    <!-- --------------------------------------------------------------------------之外------------------------ -->
    <!-- 添加分类对话框 -->
    <el-dialog
        @close="addClosed"
        title="提示"
        :visible.sync="addDialogVisible"
        width="50%">
        <!-- 内容 -->
        <el-form ref="addFormRef" :rules="addRules" :model="addForm" label-width="80px">
           <el-form-item label="分类名" prop="cat_name">
             <el-input v-model="addForm.cat_name"></el-input>
           </el-form-item>
           <el-form-item label="等级" prop="cat_level">
             <!-- 级联选择器 -->
                 <!-- options 数据源, props 指定的适配对象 -->
              <el-cascader
                 clearable
                 change-on-select
                 v-model="selectKeys"
                 :options="categoriesList"
                 expand-trigger='hover'
                 :props="categoriesProps"
                 @change="parentChange">
              </el-cascader>
           </el-form-item>
        </el-form>
        <!-- 尾巴 -->
         <span slot="footer" class="dialog-footer">
           <el-button @click="dialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
    </el-dialog>

    <!-- 编辑 对话框 -->
    <el-dialog title="提示" :visible.sync="editDialogVisible" width="30%" >
        <!-- 内容 -->
      <template>
         <el-input v-model="editInput"></el-input>
      </template>
      <!-- 尾巴 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        type: 3,
        // 当前页码
        pagenum: 1,
        // 每页显示多少个
        pagesize: 3
      },
      //   获取商品分类列表
      GoodsList: [],
      total: 0,
      //   打开对话框
      editDialogVisible: false,
      addDialogVisible: false,
      // 级联数据
      categoriesList: [],
      categoriesProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 级联 选中的ID
      selectKeys: [],
      //   table-tree
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否生效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operat'
        }
      ],
      addForm: {
        cat_name: '',
        // 父分类
        cat_pid: 0,
        cat_level: 0
      },
      addRules: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      // 编辑
      editInput: '',
      editCat_id: ''
      //   商品列表
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    //   获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      // console.log(res.data)
      if (res.meta.status !== 200) return this.$Message.error('什么都没有得到')
      this.$Message.success('得到了商品分类列表')
      this.GoodsList = res.data.result
      this.total = res.data.total
    },
    // 分页------------------------------
    //         pagesize改变 每页显示的个数                     |
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    // 当前页数 事件
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    add () {
      this.getCategories()
      this.addDialogVisible = true
    },
    // 获取级联数据
    async getCategories () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$Message.error('没有得到级联数据')
      this.categoriesList = res.data
      // console.log(res.data)
    },
    // 级联选中的状态， 把ID传出去
    parentChange () {
      // console.log(this.selectKeys.length)
      if (this.selectKeys.length > 0) {
        this.addForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addForm.cat_level = this.selectKeys.length
        // console.log(this.addForm.cat_pid)
      } else {
        this.addForm.cat_level = 0
        this.addForm.cat_pid = 0
      }
    },
    // 清空 添加对话框
    addClosed () {
      this.$refs.addFormRef.resetFields()
      this.selectKeys = []
      this.addForm.cat_pid = 0
      this.addForm.cat_level = 0
    },
    addCate () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addForm)
        if (res.meta.status !== 201) return this.$http.$Message.error('No Add')
        this.getGoodsList()
        this.addDialogVisible = false
      })
    },
    // 编辑对话框 打开
    editOpen (id) {
      this.editDialogVisible = true
      this.editCat_id = id
    },
    async edit () {
      // console.log(this.editInput)
      const { data: res } = await this.$http.put('categories/' + this.editCat_id, { cat_name: this.editInput })
      // console.log(res)
      if (res.meta.status !== 200) return this.$Message.error('别改了,出问题了')
      this.getGoodsList()
      this.$Message.success('修改完了')
      this.editDialogVisible = false
    },
    // 删除
    async deleteCate (id) {
      const confirmInfo = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmInfo)
      if (confirmInfo !== 'confirm') return this.$Message.info('取消了')
      const { data: res } = await this.$http.delete('categories/' + id)
      // console.log(res)
      if (res.meta.status !== 200) return this.$Message.error('法力值不足，删不掉')
      this.$Message.success('删掉了')
      this.getGoodsList()
    }
  }
}
</script>

<style lang="less" scoped>
  .el-cascader {
    width:100%;
  }
</style>
